﻿/*
 * Copyright (c) 2010, Sergey Loktin (mailto://shadowconsp@gmail.com)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * Licensed under The MIT License
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MoonyDesk
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        SettingsWindow _settingWnd = null;

        public MainWindow()
        {
           
            InitializeComponent();
            try
            {
                PluginSystem.ReloadPlugins();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Plugin loading error:\n" + ex.ToString()); 
            }
            PrepareSettings();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Hide();
        }

        /// <summary>
        /// prepare settings and activate plugins
        /// </summary>
        private void PrepareSettings()
        {
            //set serialization for settings to binary to store StringDictionary
            Properties.Settings.Default.Properties["xPositions"].SerializeAs = System.Configuration.SettingsSerializeAs.Binary;
            Properties.Settings.Default.Properties["yPositions"].SerializeAs = System.Configuration.SettingsSerializeAs.Binary;
            Properties.Settings.Default.Properties["movables"].SerializeAs = System.Configuration.SettingsSerializeAs.Binary;
            Properties.Settings.Default.Properties["topmosts"].SerializeAs = System.Configuration.SettingsSerializeAs.Binary;
            Properties.Settings.Default.Properties["sizes"].SerializeAs = System.Configuration.SettingsSerializeAs.Binary;
            Properties.Settings.Default.Properties["angles"].SerializeAs = System.Configuration.SettingsSerializeAs.Binary;
            Properties.Settings.Default.Properties["actives"].SerializeAs = System.Configuration.SettingsSerializeAs.Binary;
            Properties.Settings.Default.Reload();
            foreach (LoadedPlugin plugin in PluginSystem.LoadedPlugins)
            {
                //prepare setting on first program load
                if (Properties.Settings.Default.xPositions == null)
                    Properties.Settings.Default.xPositions = new System.Collections.Specialized.StringDictionary();
                if (Properties.Settings.Default.yPositions == null)
                    Properties.Settings.Default.yPositions = new System.Collections.Specialized.StringDictionary();
                if (Properties.Settings.Default.movables == null)
                    Properties.Settings.Default.movables = new System.Collections.Specialized.StringDictionary();
                if (Properties.Settings.Default.topmosts == null)
                    Properties.Settings.Default.topmosts = new System.Collections.Specialized.StringDictionary();
                if (Properties.Settings.Default.sizes == null)
                    Properties.Settings.Default.sizes = new System.Collections.Specialized.StringDictionary();
                if (Properties.Settings.Default.angles == null)
                    Properties.Settings.Default.angles = new System.Collections.Specialized.StringDictionary();
                if (Properties.Settings.Default.actives == null)
                    Properties.Settings.Default.actives = new System.Collections.Specialized.StringDictionary();

                //set null values on first load of plugin
                if (!Properties.Settings.Default.xPositions.ContainsKey(plugin.Name))
                    Properties.Settings.Default.xPositions.Add(plugin.Name, null);
                if (!Properties.Settings.Default.yPositions.ContainsKey(plugin.Name))
                    Properties.Settings.Default.yPositions.Add(plugin.Name, null);
                if (!Properties.Settings.Default.movables.ContainsKey(plugin.Name))
                    Properties.Settings.Default.movables.Add(plugin.Name, null);
                if (!Properties.Settings.Default.topmosts.ContainsKey(plugin.Name))
                    Properties.Settings.Default.topmosts.Add(plugin.Name, null);
                if (!Properties.Settings.Default.sizes.ContainsKey(plugin.Name))
                    Properties.Settings.Default.sizes.Add(plugin.Name, null);
                if (!Properties.Settings.Default.angles.ContainsKey(plugin.Name))
                    Properties.Settings.Default.angles.Add(plugin.Name, null);
                if (!Properties.Settings.Default.actives.ContainsKey(plugin.Name))
                {
                    if (plugin.Name.Equals("MoonyClock") || plugin.Name.Equals("MoonyWheather"))
                        Properties.Settings.Default.actives.Add(plugin.Name, "true");
                    else
                        Properties.Settings.Default.actives.Add(plugin.Name, "false");
                }

                //activate plugin if active state saved in setttings
                if (bool.Parse(Properties.Settings.Default.actives[plugin.Name]))
                    plugin.Activate();
            }
        }

        private void ShowSettingsWnd()
        {
            if (_settingWnd == null)
            {
                _settingWnd = new SettingsWindow();
                _settingWnd.Closed += new EventHandler(wnd_Closed);
                _settingWnd.Show();
            }
            _settingWnd.Activate();
        }

        void wnd_Closed(object sender, EventArgs e)
        {
            _settingWnd = null;
        }

        private void Options_Click(object sender, RoutedEventArgs e)
        {
            ShowSettingsWnd();
        }

        private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            ShowSettingsWnd();
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
    }
}
